﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using YunBox2.Contracts.Core;
using System.Windows;
namespace YunBox2.Client.Infrastructure.ViewModels
{
    [Export]
    public class MyPasswordEditorViewModel:DomainObject
    {
        

        [ImportingConstructor]
        public MyPasswordEditorViewModel()
        {
            _changePasswordCommand=new DelegateCommand(ChangePassword);
        }

        private string _oldPassword;
        public string OldPassword
        {
            get
            {
                return _oldPassword;
            }
            set
            {
                if(_oldPassword!=value)
                {
                    _oldPassword = value;
                    RaisePropertyChanged("OldPassword", "CanChangePassword");
                }
            }
        }

        private string _newPassword;
        public string NewPassword
        {
            get
            {
                return _newPassword;
            }
            set
            {
                if (_newPassword != value)
                {
                    _newPassword = value;
                    RaisePropertyChanged("NewPassword", "CanChangePassword");
                }
            }
        }
        private string _confrimPassword;
        public string ConfrimPassword
        {
            get
            {
                return _confrimPassword;
            }
            set
            {
                if (_confrimPassword != value)
                {
                    _confrimPassword = value;
                    RaisePropertyChanged("ConfrimPassword", "CanChangePassword");
                }
            }
        }

        protected override void Validate()
        {
            if(string.IsNullOrEmpty(_newPassword))
            {
                this["NewPassword"] = "新密码不能为空";
            }
            else
            {
                ClearError("NewPassword");
            }

            if(_newPassword!=_confrimPassword)
            {
                this["ConfrimPassword"] = "新密码与确认密码不一致";
            }
            else
            {
                ClearError("ConfrimPassword");
            }
        }

        public bool CanChangePassword
        {
            get
            {
                return !HasError;
            }
        }

        private void ChangePassword()
        {
            if(HasError)
            {
                StringBuilder errMsg=new StringBuilder();
                if(!string.IsNullOrEmpty(this["NewPassword"]))
                {
                   errMsg.AppendLine(this["NewPassword"]);
                }
                if (!string.IsNullOrEmpty(this["ConfrimPassword"]))
                {
                    errMsg.AppendLine(this["ConfrimPassword"]);
                }
                MessageBox.Show(errMsg.ToString());
                return;
            }
            using(var cs=ClientContext.Current.CreateOperationContextScope(ClientContext.Current.SystemService as IContextChannel))
            {
                if(!ClientContext.Current.SystemService.ChangeMyPassword(_oldPassword,_newPassword))
                {
                    MessageBox.Show("密码修改失败");
                }
            }
        }

        private DelegateCommand _changePasswordCommand;
        public ICommand ChangePasswordCommand
        {
            get
            {
                return _changePasswordCommand;
            }
        }
    }
}
